package zhoug.zui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;

/**
 * 印章组件
 *
 * @Author 35574
 * @Date 2020/10/27
 * @Description
 */
public class ZUISealView extends androidx.appcompat.widget.AppCompatTextView {

    private static final int DEF_BORDER_WIDTH = 3;//默认边界大小
    private int borderWidth;//印章边界的宽度
    private int borderColor;//印章边界的颜色
    private float corners;//印章边界的角度

    public ZUISealView(Context context) {
        this(context, null);
    }

    public ZUISealView(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.ZUISealView_theme);
    }

    public ZUISealView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZUISealView, defStyleAttr, R.style.ZUISealView_def);
        borderWidth = a.getDimensionPixelOffset(R.styleable.ZUISealView_zui_borderWidth, DEF_BORDER_WIDTH);
        borderColor = a.getColor(R.styleable.ZUISealView_zui_borderColor, Color.RED);
         corners = a.getDimension(R.styleable.ZUISealView_zui_corners, 5);
        int paddingStart = a.getDimensionPixelOffset(R.styleable.ZUISealView_android_paddingStart, 10);
        int paddingEnd = a.getDimensionPixelOffset(R.styleable.ZUISealView_android_paddingEnd, 10);
        int paddingTop = a.getDimensionPixelOffset(R.styleable.ZUISealView_android_paddingTop, 5);
        int paddingBottom = a.getDimensionPixelOffset(R.styleable.ZUISealView_android_paddingBottom, 5);
        float rotation = a.getFloat(R.styleable.ZUISealView_android_rotation, 15f);
        int textColor = a.getColor(R.styleable.ZUISealView_android_textColor, Color.RED);
        a.recycle();
        setPaddingRelative(paddingStart, paddingTop, paddingEnd, paddingBottom);
        if (borderWidth <= 0) {
            borderWidth = DEF_BORDER_WIDTH;
        }

        setTextColor(textColor);
        setRotation(rotation);
        _setSealBound();
    }

    private void _setSealBound() {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setStroke(borderWidth, borderColor);
        drawable.setCornerRadius(corners);
        setBackground(drawable);
    }


    public void setBorderColor(int borderColor) {
        if (borderColor != this.borderColor) {
            this.borderColor = borderColor;
            _setSealBound();
        }
    }

    public void setBorderWidth(int borderWidth) {
        if (borderWidth <= 0) {
            borderWidth = DEF_BORDER_WIDTH;
        }
        if (this.borderWidth != borderWidth) {
            this.borderWidth = borderWidth;
            _setSealBound();
        }

    }


}
